package com.qimu.saiyou.controller;

import com.qimu.saiyou.common.BaseResponse;
import com.qimu.saiyou.common.ErrorCode;
import com.qimu.saiyou.common.ResultUtils;
import com.qimu.saiyou.exception.BusinessException;
import com.qimu.saiyou.model.dto.comment.CommentLikeAddRequest;
import com.qimu.saiyou.model.entity.CommentLike;
import com.qimu.saiyou.model.entity.User;
import com.qimu.saiyou.service.CommentLikeService;
import com.qimu.saiyou.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 评论点赞
 *
 *@author: FeiXiang
 *
 */
@RestController
@RequestMapping("/comment_like")
@Slf4j
public class CommentLikeController {

    @Resource
    private CommentLikeService commentLikeService;

    @Resource
    private UserService userService;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 点赞 / 取消点赞
     *
     * @param commentLikeAddRequest
     * @param request
     * @return resultNum 本次点赞变化数
     */
    @PostMapping("/")
    public BaseResponse<Integer> doCommentLike(@RequestBody CommentLikeAddRequest commentLikeAddRequest,
                                               HttpServletRequest request) {
        if (ObjectUtils.allNull(commentLikeAddRequest, commentLikeAddRequest.getCommentId(), commentLikeAddRequest.getPostId()) || commentLikeAddRequest.getPostId() <= 0 || commentLikeAddRequest.getCommentId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能点赞
        final User loginUser = userService.getLoginUser(request);
        long commentId = commentLikeAddRequest.getCommentId();
        Long postId = commentLikeAddRequest.getPostId();
        int result = commentLikeService.doCommentLike(commentId, postId, loginUser, request);
        redisTemplate.delete("commentListPageByPostId" + postId);
        return ResultUtils.success(result);
    }

    @PostMapping("/list")
    public BaseResponse<List<Long>> commentLikeList(@RequestBody CommentLikeAddRequest commentLikeAddRequest,
                                                    HttpServletRequest request) {
        if (ObjectUtils.allNull(commentLikeAddRequest, commentLikeAddRequest.getPostId()) || commentLikeAddRequest.getPostId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能点赞
        final User loginUser = userService.getLoginUser(request);
        Long postId = commentLikeAddRequest.getPostId();
        List<Long> likeList = commentLikeService
                .lambdaQuery()
                .eq(CommentLike::getUserId, loginUser.getId())
                .eq(CommentLike::getPostId, postId)
                .list()
                .stream()
                .map(CommentLike::getCommentId)
                .collect(Collectors.toList());
        return ResultUtils.success(likeList);
    }

}
